$(function(){
	getSpecialsList();
	getActivityTotalList();
	getActivityList();
	getPlateProductList();
});

function getSpecialsList() {
	var url = '/json?action=QUERY_T_OFFER_PRODUCT_BY_SPECIALS_ACTION';
	$UI.listPage(url,'T_OFFER_PRODUCT','#specialsList',printSpecialsRes,'');
}

function getActivityTotalList() {
	var url = '/json?action=QUERY_T_ACTIVITY_ACTION';
	$UI.listPage(url,'T_ACTIVITY','#activityList',printActivityTotalRes,'');
}

function getActivityList() {
	var url = '/json?action=QUERY_T_ACTIVITY_ACTION';
	$UI.listPage(url,'T_ACTIVITY','#activityList',printActivityRes,'');
}

function getPlateProductList() {
	var url = '/json?action=QUERY_T_PLATE_PRODUCT_ACTION';
	$UI.listPage(url,'T_PLATE_PRODUCT','#madBuyList',printPlateProductRes,'');
}

function printSpecialsRes(obj,result,i,n,total) {
	var divStr = [
		'<li>',
		'<a href="/product/file.html?id=',
		obj.PRODUCT_ID,
		'" title=',obj.PRODUCT_NAME,'>',
		'<img src="upload/product/',
		obj.PRODUCT_NO,
		'.jpg" width="120" heigth="120"/>',
		'</a>',
		'<p class="tc">',
		'<a href="/product/file.html?id=',
		obj.PRODUCT_ID,
		'" title=',obj.PRODUCT_NAME,'>',
		$Util.ellipsis(obj.PRODUCT_NAME,10,''),
		'</a>',
		'</p>',
		'<p>',
		'<span>￥',
		obj.OFFER_PRICE,
		'</span>',
		'<strong>',
		$Math.fix((obj.OFFER_PRICE/obj.MARKET_PRICE)*10,1),
		'折',
		'</strong>',
		'</p>',
		'</li>'
	].join('');
	
	$(result).append(divStr);
	if (i+1 == total) {
		$(result).bxCarousel({
			display_num : 4,
			move : 1,
			margin : 40
		});
	}
}

function printActivityTotalRes(obj,result,i,n,total) {
	if (i+1>6) return;
	var divStr = [
		'<li>',
		'<div class="sp_text">',
		'<h5>',
		obj.ACTIVITY_TITLE,
		'</h5>',
		'<p>',
		'<strong>￥2000.00</strong>',
		'<font>￥2400.00</font>',
		'</p>',
		'<p>',
		'<a href="#" class="cGray">',
		obj.ACTIVITY_DESC,
		'</a>',
		'</p>',
		'</div>',
		'<div class="sp_img ld">',
		(getActivityCss(obj.ACTIVITY_TYPE) == '' ? '' : getActivityCss(obj.ACTIVITY_TYPE)),
		'<img src="',
		obj.ACTIVITY_PHOTO,
		'" width="100" height="100"/>',
		'</div>',
		'</li>'
	].join('');
	if ($('#activityUpList >li').length>2) {
		$('#activityDownList').append(divStr);
	} else {
		$('#activityUpList').append(divStr);
	}
}

function printActivityRes(obj,result,i,n,total) {
	if (i+1>6) return;
	var divStr = [
		'<li>',
		'<div class="sp_text">',
		'<h5>',
		obj.ACTIVITY_TITLE,
		'</h5>',
		'<p>',
		'<strong>￥2000.00</strong>',
		'<font>￥2400.00</font>',
		'</p>',
		'<p>',
		'<a href="#" class="cGray">',
		obj.ACTIVITY_DESC,
		'</a>',
		'</p>',
		'</div>',
		'<div class="sp_img ld">',
		'<img src="',
		obj.ACTIVITY_PHOTO,
		'" width="100" height="100"/>',
		'</div>',
		'</li>'
	].join('');
	// 买赠
	if (obj.ACTIVITY_TYPE == 1) {
		if ($('#buyGiftUpList >li').length>2) {
			$('#buyGiftDownList').append(divStr);
		} else {
			$('#buyGiftUpList').append(divStr);
		}
	// 买减
	} else if (obj.ACTIVITY_TYPE == 2) {
		if ($('#buyLessUpList >li').length>2) {
			$('#buyLessDownList').append(divStr);
		} else {
			$('#buyLessUpList').append(divStr);
		}
	// 满赠
	} else if (obj.ACTIVITY_TYPE == 3) {
		if ($('#FullGiftUpList >li').length>2) {
			$('#FullGiftDownList').append(divStr);
		} else {
			$('#FullGiftUpList').append(divStr);
		}
	// 满减
	} else if (obj.ACTIVITY_TYPE == 4) {
		if ($('#FullLessUpList >li').length>2) {
			$('#FullLessDownList').append(divStr);
		} else {
			$('#FullLessUpList').append(divStr);
		}
	}
}

function getActivityCss(activityType) {
	var css = '';
	if (activityType == 1) {
		css = '<b class="buy_add"></b>';
	} else if (activityType == 2) {
		css = '<b class="buy_cut"></b>';
	} else if (activityType == 3) {
		css = '<b class="full_add"></b>';
	} else if (activityType == 4) {
		css = '<b class="full_cut"></b>';
	}
	return css;
}

function printPlateProductRes(obj,result,i,n,total) {
	var tag;
	var desc;
	switch(obj.PLATE_CODE) {
		// 疯狂抢购
		case '1011':
			result = '#madBuyList';
			break;
		// 新品上市
		case '1012':
			result = '#newList';
			break;
		// 热销商品
		case '1013':
			result = '#hotItemList';
			break;
		// 推荐商品
		case '1014':
			result = '#recommendList';
			break;
		// 宝宝营养
		case '1031':
			result = '#foodList';
			tag = '#foodTag';
			desc = '宝宝营养';
			break;
		// 宝宝扮靓
		case '1032':
			result = '#dressList';
			tag = '#dressTag';
			desc = '宝宝扮靓';
			break;
		// 宝宝用品
		case '1033':
			result = '#articlesList';
			tag = '#articlesTag';
			desc = '宝宝用品';
			break;
		// 宝宝娱乐
		case '1034':
			result = '#toyList';
			tag = '#toyTag';
			desc = '宝宝娱乐';
			break;
		// 孕产妇
		case '1035':
			result = '#motherPList';
			tag = '#msTag';
			desc = '孕产妇';
			break;
		default:
			result = '';
			break;
	}
	
	if (tag && desc && obj.PLATE_TAG) {
		var tags = obj.PLATE_TAG.split(',');
		if (tags.length > 0) {
			var tagStr = '热销商品：';
			$.each(tags,function(m,n){
				tagStr += [
					'<a href="/product/list.html?skeyword=',
					encodeURI(n),
					'">',
					n,
					'</a>'
				].join('');
			});
			$(tag).html(tagStr);
		}
	}
	
	if (result && obj.PRODUCT_LIST.length>0) {
		var appendStr = '';
		$.each(obj.PRODUCT_LIST,function(j,m){
			appendStr += [
				'<li>',
				'<a href="/product/file.html?id=',
				m.productId,
				'" title=',m.productName,'>',
				'<img src="/upload/product/',
				m.productNo,
				'.jpg" width="160" height="160"/>',
				'</a>',
				'<p>',
				'<a href="/product/file.html?id=',
				m.productId,
				'" title=',m.productName,'>',
				$Util.ellipsis(m.productName,13,''),
				'</a>',
				'</p>',
				'<p class="cDGray">金麒价：',
				'<strong>￥',
				m.memberPrice,
				'</strong>',
				'</p></li>'
			].join('');
		});
		$(result).append(appendStr);
	}
	if (i+1 == total) {
		tabs('.tab_hd', 'active', '.tab_bd');
	}
}

function tabs(tabTit, on, tabCon) {
	$(tabCon).each(function() {
				$(this).children().eq(0).show();
			});
	$(tabTit).each(function() {
				$(this).children().eq(0).addClass(on);
			});
	$(tabTit).children().hover(function() {
				$(this).addClass(on).siblings().removeClass(on);
				var index = $(tabTit).children().index(this);
				$(tabCon).children().eq(index).show().siblings().hide();
			});
}
